<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
require_once("lib/validate.php");
//Includes
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("components4phpfull/jtiframe.inc.php");
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("components4phpfull/jtjswindow.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class register_company extends Page
{
       public $lblEmail = null;
       public $lblcompphone = null;
       public $lblAddress = null;
       public $hfError = null;
       public $JSValidate = null;
       public $lblCompanyName = null;
       public $imgCaptchaRefresh = null;
       public $HiddenRandom = null;
       public $editCaptcha = null;
       public $Label15 = null;
       public $img2 = null;
       public $btnAddG = null;
       public $JTBranch = null;
       public $Label14 = null;
       public $Image3 = null;
       public $Label12 = null;
       public $hiddenAuto = null;
       public $Button2 = null;
       public $hiddenNewCount = null;
       public $hiddenExist = null;
       public $hiddenComp = null;
       public $JTContact = null;
       public $Button1 = null;
       public $Label4 = null;
       public $Image2 = null;
       public $JTSiteTheme1 = null;
       public $edApnum = null;
       public $Label3 = null;
       public $edcompfax = null;
       public $edcompphone = null;
       public $Label29 = null;
       public $Label23 = null;
       public $uploadLogo = null;
       public $lblPhoto = null;
       public $edIndustry = null;
       public $Label11 = null;
       public $edBtype = null;
       public $hiddenUserError = null;
       public $JSUserUnique = null;
       public $GroupBox1 = null;
       public $btnCheck = null;
       public $lblUsernameError = null;
       public $lblPasswordError = null;
       public $Label10 = null;
       public $Label9 = null;
       public $Label8 = null;
       public $edPassword2 = null;
       public $edPassword = null;
       public $edUsername = null;
       public $Label7 = null;
       public $Label5 = null;
       public $Label2 = null;
       public $edWebsite = null;
       public $edEmail = null;
       public $edTaxIdNum = null;
       public $cmbBusiness = null;
       public $Label13 = null;
       public $Label28 = null;
       public $cmbMunicipality = null;
       public $Label27 = null;
       public $cmbProvince = null;
       public $Label26 = null;
       public $cmbRegion = null;
       public $edBldg = null;
       public $Label25 = null;
       public $edAddress = null;
       public $Label24 = null;
       public $Image1 = null;
       public $lblError = null;
       public $btnCancel = null;
       public $btnSubmit = null;
       public $Label6 = null;
       public $edSlogan = null;
       public $Label1 = null;
       public $edCompanyName = null;
       function edEmailJSBlur($sender, $params)
       {

       ?>
       validateRequire('edEmail','lblEmail');
       <?php

       }

       function edcompphoneJSBlur($sender, $params)
       {

       ?>
       validateRequire('edcompphone','lblcompphone');
       <?php

       }

       function edAddressJSBlur($sender, $params)
       {

       ?>
       validateRequire('edAddress','lblAddress');
       <?php

       }
       function edCompanyNameJSBlur($sender, $params)
       {

       ?>
       validateRequire('edCompanyName','lblCompanyName');

       <?php

       }

       function validateRequired($value,$lbl)
       {
        if($value=='' || $value==null)
        {
         $this->$lbl->Caption = '&darr This field is required &darr';
         $this->hfError->setValue('1');
        }
       }

       function imgCaptchaRefreshJSClick($sender, $params)
       {

       ?>
       document.images.img2.src = 'php_captcha.php?'+Math.random();
       <?php

       }

       function register_companyJSLoad($sender, $params)
       {

       ?>
       document.images.img2.src = 'php_captcha.php'

       <?php

       }

       function btnAddGJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var comp = document.getElementById("hiddenComp").value;
       var rand = document.getElementById("HiddenRandom").value;
       var win_height = 304;
       var win_width = 368;
       var height = window.opener.outerHeight;
       var width = window.opener.outerWidth;
       var top = Math.round((height-win_height)/2);
       var left = Math.round((width-win_width)/2);
       window.open("comp_garage_create.php?comp_id="+comp,'garage_create','location=no,height='+win_height+',width='+win_width+',top='+top+',left='+left);
       <?php

       }

       function Button2JSClick($sender, $params)
       {
       ?>
       var auto = document.getElementById("hiddenAuto").value;
       document.getElementById("edApnum").value = auto;

       <?php

       }

       function Button1JSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var comp = document.getElementById("hiddenComp").value;
       var rand = document.getElementById("HiddenRandom").value;
       var win_height = 364;
       var win_width = 744;
       var height = window.opener.outerHeight;
       var width = window.opener.outerWidth;
       var top = Math.round((height-win_height)/2);
       var left = Math.round((width-win_width)/2);
       window.open("comp_contact_create.php?comp_id="+comp,'contact_create','location=no,height='+win_height+',width='+win_width+',top='+top+',left='+left);
       //window.open("comp_contact_create.php?comp_id="+comp,"Window2",  "menubar=no,width=744,height=304,left=300,top=230,toolbar=no");
       return false;
       <?php

       }


       function btnCheckClick($sender, $params)
       {
       $this->checkUniqueUser();
       }


       function checkUniqueUser()
       {
       $query_unique = "SELECT username FROM accounts_tbl WHERE username='".$this->edUsername->Text."'";
       $result_unique = mysql_query($query_unique) or die("UNIQUE ".mysql_error());
       $count_unique = mysql_num_rows($result_unique);

       if($count_unique>0)
       {
        $this->lblUsernameError->Caption = 'This username is not available';
        $this->lblUsernameError->Font->Color = 'Red';
	      $this->hiddenUserError->setValue('1');

        $this->edPassword->Enabled = false;
        $this->edPassword2->Enabled = false;
        $this->edPassword->Color = '#C0C0C0';
        $this->edPassword2->Color = '#C0C0C0';
       }
       if($count_unique==0 && $this->edUsername->Text!='')
       {
        $this->lblUsernameError->Caption = 'Username Available';
        $this->lblUsernameError->Font->Color = 'Blue';
	      $this->hiddenUserError->setValue('0');

        $this->edPassword->Enabled = true;
        $this->edPassword2->Enabled = true;
        $this->edPassword->Color = '#FFFFFF';
        $this->edPassword2->Color = '#FFFFFF';
       }
       if($this->edUsername->Text=='')
       {
        $this->lblUsernameError->Caption = 'Please type a valid username';
        $this->lblUsernameError->Font->Color = 'Red';
	      $this->hiddenUserError->setValue('1');

        $this->edPassword->Enabled = false;
        $this->edPassword2->Enabled = false;
        $this->edPassword->Color = '#C0C0C0';
        $this->edPassword2->Color = '#C0C0C0';
       }
       }


       function cmbProvinceJSChange($sender, $params)
       {
       echo $this->cmbProvince->ajaxCall("VendorProvinceChange");
       ?>
       //alert(document.getElementById('cmbProvince').value);
       return false;
       <?php

       }

       function cmbRegionJSChange($sender, $params)
       {
       echo $this->cmbRegion->ajaxCall("VendorRegionChange");
       ?>
       return false;

       <?php

       }

       function btnCancelJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       top.location="login.php";

       <?php

       }

       function btnSubmitJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       window.alert('successfully created user!')
       top.location="login.php";
       <?php

       }



       function btnSubmitClick($sender, $params)
       {
       /*$validator = new validate();
       $fields = array();
       $fields = array($this->edCompanyName->Text,
       $this->edAddress->Text,
       $this->edcompphone->Text,
       $this->edEmail->Text);*/
       //$this->edLname->Text,
       //$this->edFname->Text,
       //$this->edPhone->Text,
       //$this->edUsername->Text,
       //$this->edPassword->Text,
       //$this->edPassword2->Text);

       /*if($this->edPassword->Text!=$this->edPassword2->Text)
        {
         $this->lblPasswordError->setCaption('Passwords did not match');
         $this->lblError->setCaption('Passwords did not match');
        }*/
       //$this->checkUniqueUser();

       /*if($validator->validateRequiredFields($fields)==true && $number==$key)
       {
       if($this->edPassword->Text!=$this->edPassword2->Text)
        {
         $this->lblPasswordError->setCaption('Passwords did not match');
         $this->lblError->setCaption('Passwords did not match');
         redirect('register_company.php');
        }*/

       /*if($this->edPassword->Text=='')
       {
       $this->edPassword->Text='1234';
       $this->edPassword2->Text='1234';
       }
       else
       {
       $this->edPassword->Text;
       }*/

       /*$result_company_id = mysql_query("SELECT MAX(company_id) as max FROM company_tbl") or die("SELECT COMPANY ID - ".mysql_error());
       $row_company_id = mysql_fetch_array($result_company_id);
       $company_id = $row_company_id['max']+1;*/

       $key=substr($_SESSION['key'],0,5);
       $number = $this->editCaptcha->Text;
       if($number!=$key)
       {
        //die('xxx');
        echo "<script type=text/javascript>
        alert('Please enter the correct string shown in the image.');
        </script>";
       }
       $this->validateRequired($this->edCompanyName->Text,'lblCompanyName');
       $this->validateRequired($this->edAddress->Text,'lblAddress');
       $this->validateRequired($this->edcompphone->Text,'lblcompphone');
       $this->validateRequired($this->edEmail->Text,'lblEmail');
       $error = $this->hfError->getValue();
       if($error=='1' || $number!=$key)
        {
         $this->hfError->setValue('0');
         return false;
        }
       else if($error=='0')
        {
       $result_company_count2 = mysql_query("SELECT * FROM lookup_company_count")
       or die("comp count2 ".mysql_error());
       $row_comp_count2 = mysql_fetch_array($result_company_count2);
       $number1 = $row_comp_count2['total_comp_count']+1;
       $number2 = 'AMPS00000'.$number1;



       mysql_query("INSERT INTO company_tbl (company_name,
       app_num,
       tax_idno,
       address,
       bldg,
       region,
       province,
       municipality,
       business_type,
       comp_slogan,
       website,
       comp_phone,
       comp_fax,
       general_email,
       photo)
       VALUES
       ('".$this->edCompanyName->Text."',
       '".$number2."',
       '".$this->edTaxIdNum->Text."',
       '".$this->edAddress->Text."',
       '".$this->edBldg->Text."',
       '".$this->cmbRegion->getItemIndex()."',
       '".$this->cmbProvince->getItemIndex()."',
       '".$this->cmbMunicipality->getItemIndex()."',
       '".$this->edBtype->Text."',
       '".$this->edSlogan->Text."',
       '".$this->edWebsite->Text."',
       '".$this->edcompphone->Text."',
       '".$this->edcompfax->Text."',
       '".$this->edEmail->Text."',
       '"."photo/company/logo".$company_id.".".$this->uploadLogo->FileExt."')")or die("INSERT COMPANY - ".mysql_error());

       $this->uploadLogo->moveUploadedFile("photo/company/logo".$company_id.".".$this->uploadLogo->FileExt);

       /*if($this->chkAdmin->Checked==true)
       {
       $admin='1';
       }
       if($this->chkAdmin->Checked==false)
       {
       $admin='0';
       }*/

       /*mysql_query("INSERT INTO accounts_tbl (username, password, company_id, company_name, status, admin)
       VALUES
       ('".$this->edUsername->Text."',
       '".sha1($this->edPassword->Text)."',
       '".$company_id."',
       '".$this->edCompanyName->Text."',
       '0',
       '1'
       )") or die("INSERT ACCOUNT - ".mysql_error());*/
       $result_company_id = mysql_query("SELECT LAST_INSERT_ID(company_id) AS comp_id FROM company_tbl ORDER BY company_id DESC LIMIT 0,1") or die("SELECT COMPANY ID - ".mysql_error());
       $row_company_id = mysql_fetch_array($result_company_id);
       $company_id = $row_company_id['comp_id'];


       mysql_query("INSERT INTO accounts_tbl (username, company_id, company_name, status, admin)
       VALUES
       ('".$this->edEmail->Text."',
       '".$company_id."',
       '".$this->edCompanyName->Text."',
       '0',
       '1'
       )") or die("INSERT ACCOUNT - ".mysql_error());

       $query_up_cont = "UPDATE company_contact SET company_id='".$company_id."' WHERE company_id='".$_SESSION['rand']."'";
        mysql_query($query_up_cont) or die("UPDATE CONTACTS ".mysql_error());

       $query_up_garage = "UPDATE company_garage SET company_id='".$company_id."' WHERE company_id='".$_SESSION['rand']."'";
        mysql_query($query_up_garage) or die("UPDATE GARAGE ".mysql_error());

       //start auto-generate vin (note:INSERT STATEMENT)
       //die($this->hiddenExist->getValue());
       if($this->hiddenExist->getValue()=='0')
       {
       mysql_query("INSERT INTO lookup_company_count (total_comp_count)
       VALUES ('1')")
       or die("INSERT COUNT ".mysql_error());
       }
       if($this->hiddenExist->getValue()=='1')
       {
       mysql_query("UPDATE lookup_company_count SET total_comp_count='".$number1."'")
       or die("UPDATE COUNT ".mysql_error());;
       }
       //end auto-generate vin

       echo "<script language=javascript>
       alert('Registration Successful!Kindly wait for the approval of your account.Thank you!')
       //top.location='login.php';
       window.close();
       </script>";
       //redirect('login.php');
       /*}
       else
       {
        $err_field1 = $validator->validateRequired($this->edCompanyName->Text, 'Company Name, ');
        $err_field2 = $validator->validateRequired($this->edAddress->Text, 'Address, ');
        $err_field3 = $validator->validateRequired($this->edcompphone->Text, 'Phone No., ');
        $err_field4 = $validator->validateRequired($this->edEmail->Text, 'Email, ');;

        $error_fields = $err_field1.$err_field2.$err_field3.$err_field4;
        if($validator->validateRequiredFields($fields)==false)
        $error_message = $validator->addRequiredMessage($error_fields);
        $this->lblError->setCaption($error_message);

       }*/


       }
       }





       function VendorRegionChange()
       {
       $region_id = $this->cmbRegion->getItemIndex();
       $this->cmbProvince->Clear();
       $this->cmbProvince->AddItem('--Select--', null, null);
       $result_province = mysql_query("select * from lookup_province where region_id='".$region_id."'");

       while($row_province = mysql_fetch_array($result_province))
       {
            $this->cmbProvince->AddItem($row_province['province'], null, $row_province['id']);
       }

       if($region_id==0)
       {
        $this->cmbProvince->Clear();
        $this->cmbMunicipality->Clear();
        $this->cmbProvince->AddItem('--Select Region First--',null,null);
        $this->cmbMunicipality->AddItem('--Select Province First--',null,null);
       }
       }


       function VendorProvinceChange()
       {
       $region_id = $this->cmbRegion->getItemIndex();
       $this->cmbProvince->Clear();
       $this->cmbProvince->AddItem('--Select--', null, null);
       $result_province = mysql_query("select * from lookup_province where region_id='".$region_id."'");

       while($row_province = mysql_fetch_array($result_province))
       {
            $this->cmbProvince->AddItem($row_province['province'], null, $row_province['id']);
       }

       $province_id = $this->cmbProvince->getItemIndex();
       $this->cmbMunicipality->Clear();
       $this->cmbMunicipality->AddItem('--Select--', null, null);
       $query_municipality = "select * from lookup_municipality where province_id='".$province_id."'";
       $result_municipality = mysql_query($query_municipality);
       //die($query_municipality);
       while($row_municipality = mysql_fetch_array($result_municipality))
       {
            $this->cmbMunicipality->AddItem($row_municipality['municipality'], null, $row_municipality['id']);
       }

       if($province_id==0)
       {
        $this->cmbMunicipality->Clear();
        $this->cmbMunicipality->AddItem('--Select Province First--',null,null);
       }
       }


       function register_companyCreate($sender, $params)
       {
       $this->hfError->setValue('0');
       $this->lblCompanyName->Caption = '';
       $this->lblAddress->Caption = '';
       $this->lblcompphone->Caption = '';
       $this->lblEmail->Caption = '';

       if(!isset($_SESSION['rand']))
       {
       $_SESSION['rand'] = rand() . "\n";
       }


       //$this->HiddenRandom->setValue($random);
       //$this->lblPasswordError->setCaption('');
       /*echo "<script>
       alert('1234');
       </script>";*/

       //CHECK USERNAME's AVAILABILITY FIRST
       //$this->edPassword->Enabled = false;
       //$this->edPassword2->Enabled = false;
       //$this->edPassword->Color = '#C0C0C0';
       //$this->edPassword2->Color = '#C0C0C0';
       //CHECK U A F

       //$this->lblUsernameError->setCaption('');
       //$this->lblPasswordError->setCaption('');
       //$this->lblError->setCaption('');
       $this->hiddenComp->setValue(0);

       $this->JTContact->setURL('comp_contact_list.php?company_id='.$this->hiddenComp->getValue());
       $this->JTBranch->setURL('comp_garage_list.php?company_id='.$this->hiddenComp->getValue());

       $this->edCompanyName->Text='';
       $this->edTaxIdNum->Text='';
       $this->edAddress->Text='';
       $this->edBldg->Text='';
       //$this->edLname->Text='';
       //$this->edFname->Text='';
       //$this->edMname->Text='';
       //$this->edMobile->Text='';
       //$this->edPhone->Text='';
       //$this->edFax->Text='';
       //$this->edSlogan->Text='';
       $this->edWebsite->Text='';
       $this->edEmail->Text='';
       //$this->edUsername->Text='';
       //$this->edPassword->Text='';
      // $this->edPassword2->Text='';
       $this->edApnum->Text='';
       $this->edcompphone->Text='';
       $this->editCaptcha->Text='';

      //start auto-generate vin
       //hiddenExist
       //hiddenNewCount
       $concat_string = 'AMPS';
       $result_company_count = mysql_query("SELECT * FROM lookup_company_count")
       or die("comp count ".mysql_error());

       if(mysql_num_rows($result_company_count)==0)
       {
        $this->hiddenAuto->setValue($concat_string.'000001');
        $this->hiddenExist->setValue('0');
       }
       if(mysql_num_rows($result_company_count)>0)
       {
        $row_comp_count = mysql_fetch_array($result_company_count);
        $number = $row_comp_count['total_comp_count']+1;

        $this->hiddenNewCount->setValue($number);

        $length = strlen($number);
        $num_zero = 6 - $length;
        $zero = '';
        while($num_zero>0)
         {
           $zero = $zero.'0';
           $num_zero--;
         }
        $this->hiddenAuto->setValue($concat_string.$zero.$number);
        //$this->edApnum->Text = $concat_string.$zero.$number;
        $this->hiddenExist->setValue('1');
       }
       //end auto-generate vin




       //region,province,municipality
       $this->cmbRegion->Clear();
       $this->cmbRegion->AddItem('--Select--', null, null);
       $result_vend_region = mysql_query("select * from lookup_region");

        while($row_vend_region = mysql_fetch_array($result_vend_region))
        {
            $this->cmbRegion->AddItem($row_vend_region['region'], null, $row_vend_region['id']);
        }
        if($this->cmbProvince->getItemIndex()==-1)
        {
        $this->cmbProvince->Clear();
        $this->cmbProvince->AddItem('--Select Region First--',null,null);
        }
        if($this->cmbProvince->getItemIndex()==-1)
        {
        $this->cmbMunicipality->Clear();
        $this->cmbMunicipality->AddItem('--Select Province First--',null,null);
        }
       }


       /*function btnCancelClick($sender, $params)
       {
       redirect('login.php?');
       } */

}

global $application;

global $register_company;

//Creates the form
$register_company=new register_company($application);

//Read from resource file
$register_company->loadResource(__FILE__);

//Shows the form
$register_company->show();

?>